package name.jautzy.esi.client;

import java.util.ArrayList;
import java.util.List;

import name.jautzy.esi.client.model.EsiMessages;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.Padding;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer; 
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.Style;

import com.google.gwt.user.client.Element; 

public class EsiLayoutContainer extends LayoutContainer {
	private ContentPanel west;
	private ContentPanel center;
	
	public EsiLayoutContainer() {
		super();
	}
	
	@Override
	protected void onRender(Element target, int index) {  
	    super.onRender(target, index);
	    this.setLayout(new FitLayout());
	    ContentPanel panel = new ContentPanel();
	    panel.setHeading("Eve Ship Identity v."+Esi.version);
	    panel.setLayout(new BorderLayout());
	    
	    BorderLayoutData messageData = new BorderLayoutData(LayoutRegion.SOUTH, 100);  
	    messageData.setMargins(new Margins(5));  
	    messageData.setSplit(true); 
	    messageData.setCollapsible(true);
	    
	    panel.add(getMessagePanel(),messageData);
	    
	    ContentPanel north = new ContentPanel();
	    north.setLayout(new FitLayout());
	    north.setHeaderVisible(false);
	    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 30);  
	    northData.setMargins(new Margins(5));  
	    northData.setSplit(false); 
	    northData.setCollapsible(false);
	    ToolBar toolBar = new ToolBar();
	    toolBar.setBorders(false);
	    Status welcome = new Status();
	    welcome.setText("Welcome "+Esi.getEsi().getLoginInfo().getNickname());
	    welcome.setWidth(150);
	    
	    Status logout = new Status();
	    logout.setText(Esi.getSignOutLink().toString());
	    toolBar.add(welcome);
	    toolBar.add(new FillToolItem());
	    toolBar.add(logout);
	    north.add(toolBar);
	    panel.add(north,northData);
	    
	    west = new ContentPanel();  
	    west.setHeading("Modules");  
	    VBoxLayout westLayout = new VBoxLayout();  
	    westLayout.setPadding(new Padding(0));  
	    westLayout.setVBoxLayoutAlign(VBoxLayoutAlign.STRETCH);  
	    west.setLayout(westLayout);  
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150, 100, 250);  
	    westData.setMargins(new Margins(5));  
	    westData.setSplit(true); 
	    westData.setCollapsible(true); 
	    panel.add(west, westData); 
	    
	    center = new ContentPanel();  
	    center.setHeaderVisible(false);  
	    center.setBodyStyle("background-color:black;"
				  +"background-image:url(ressources/images/background/01_1024.jpg);"
				  +"backgroung-repeat:no-repeat;"
				  +"color:white");
	    center.setLayout(new FitLayout());  
	    center.add(new Html(  
	        "<p style=\"padding:10px;color:#556677;font-size:11px;\">Select a button in the left</p>"));  
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5));  
	    panel.add(center, centerData);    
	    
	    west.add(createToggleButton("All Fits", new DisplayFitGridListener(center,false)));
	    west.add(createToggleButton("My Fits", new DisplayFitGridListener(center,true)));
	    west.add(createToggleButton("Eve Database", new DisplayDbListener(center))); 
	    west.add(createToggleButton("Preferences", new PreferencesListener(center))); 
	    west.add(createToggleButton("About", new AboutListener(center))); 
	    
	    add(panel, new FlowData(10));  

	}
	
	private ToggleButton createToggleButton(String name, Listener<ButtonEvent> l) {  
	    ToggleButton button = new ToggleButton(name);  
	    button.setToggleGroup("vboxlayoutbuttons");  
	    button.addListener(Events.Toggle, l);  
	    button.setAllowDepress(false);  
	    return button;  
	  } 
	
	private ContentPanel getMessagePanel() {
		ContentPanel panel = new ContentPanel();
		panel.setLayout(new FitLayout());
		panel.setHeading("Messages");
		
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		  
	    ColumnConfig column = new ColumnConfig("time", "Timestamp",200);  
	    configs.add(column);  
	  
		column = new ColumnConfig("message", "Message", 500);  
	    configs.add(column);  
	    
	    ColumnModel cm = new ColumnModel(configs);  
	    
	    ListStore<EsiMessages> store = Esi.getEsi().getMessages(); 
	    store.setSortField("time");
	    store.setSortDir(Style.SortDir.DESC);
	    
	    Grid<EsiMessages> grid = new Grid<EsiMessages>(store, cm); 
	    panel.add(grid);
		
		return panel;
	}

}
